package stream;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class TestCollectorMap {
    public static void main(String[] args) {
        List<People<String, Integer>> list = new ArrayList<>();
        list.add(new People<>("wyl", 18));
        list.add(new People<>("wyl", 23));

        // 仅仅会创建对应的op对象，但是不会执行其中的逻辑
        // Stream<Integer> integerStream = list.stream().map(People::getAge).filter(x -> x % 2 == 0);

        List<Integer> ageList = list.stream().map(People::getAge).filter(x -> x % 2 == 0).collect(Collectors.toList());
                //.forEach(System.out::println);

//        Map<String, Integer> map = list.stream().collect(Collectors.toMap(People::getName, People::getAge, BinaryOperator.maxBy(Comparator.comparingInt(x -> x))));
//        System.out.println(map.get("wyl"));

        // Exception in thread "main" java.lang.IllegalStateException: Duplicate key 18
        // Map<String, Integer> map = list.stream().collect(Collectors.toMap(stream.People::getName, stream.People::getAge));
    }
}

class People<String, Integer> {
    private String name;
    private Integer age;

    public People(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public java.lang.String toString() {
        return "stream.People{" +
                "name=" + name +
                ", age=" + age +
                '}';
    }
}
